package cn.edu.nuc.onlinestore.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import cn.edu.nuc.onlinestore.util.GoodsUpdateutil;
import cn.edu.nuc.onlinestore.util.UserBuyutil;
import cn.edu.nuc.onlinestore.vo.Goods;

import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class UserStore extends JFrame {
	static UserCartFrame cf;
	private JPanel contentPane;
	private JTextField selectext2;
	static UserGoods d;
	List<Goods> list = new ArrayList<Goods>();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserStore frame = new UserStore("");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UserStore(String name) {
		String sname="";
		setTitle("中北在线商场--当前用户: "+name);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 732, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 78, 696, 341);
		panel.setLayout(new GridLayout(1, 1, 0, 0));
		
		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号");
		model.addColumn("名称");
		model.addColumn("单价(人民币)");
		model.addColumn("库存");
		

		/**
		 * 接受服务端传来商品list
		 * 显示商品
		 * 
		 */
		
		new Thread(new Runnable(){

			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					UserBuyutil ub=new UserBuyutil();
					list=ub.getList();
				}
				
			}
			
		}).start();
		
		
		//model.addColumn("操作");
		if(sname.equals("")){
//			    UserBuyutil ub=new UserBuyutil();
//			    list=ub.getList();
				for(int i=0;i<list.size();i++){
					
					Goods g=list.get(i);
					
					model.addRow(new String[]{String.valueOf(g.getId()),g.getName(),String.valueOf(g.getPrice()),String.valueOf(g.getNum())});
				}
		
		}
		JTable table = new JTable( model );
		
		JScrollPane pane = new JScrollPane( table );
		
		panel.add(pane);
		contentPane.add(panel);
		
		JButton button_2 = new JButton("查看商品详细信息(或双单击商品列)");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				
					int selectRow=table.getSelectedRows().length;
					System.out.println(selectRow);

					DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

					if(selectRow==1){
					  int selectedRowIndex = table.getSelectedRow(); // 取得用户所选单行 
					  
					  Goods g=list.get(selectedRowIndex);
					 
					  d = new UserGoods(g);
					  d.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
					  d.setVisible(true);
				
					}else{
						JOptionPane.showMessageDialog(null, "请选择单行内容", "alert", JOptionPane.ERROR_MESSAGE); 
					}
				
			}
		});
		button_2.setBounds(407, 45, 299, 23);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("退出登录");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UserBuyutil uc=new UserBuyutil();
				if(uc.Send(name)){
					JOptionPane.showMessageDialog(null, "您已下线", "alert", JOptionPane.ERROR_MESSAGE);
					System.exit(0);
				}
			}
		});
		button_3.setBounds(613, 6, 93, 23);
		contentPane.add(button_3);
		
		JLabel lblid = new JLabel("商品名称:");
		lblid.setBounds(10, 53, 65, 15);
		contentPane.add(lblid);
		
		selectext2 = new JTextField();
		selectext2.setBounds(85, 46, 104, 21);
		contentPane.add(selectext2);
		selectext2.setColumns(10);
		
		JButton button_4 = new JButton("搜索");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				model.setRowCount(0);
				if(selectext2.getText().equals("")){
					
					for(int i=0;i<list.size();i++){
						list.get(i);
						//list.add(g);
						model.addRow(new String[]{String.valueOf(list.get(i).getId()),list.get(i).getName(),String.valueOf(list.get(i).getPrice()),String.valueOf(list.get(i).getNum())});
					}
			}else{
				  
				    for(int i=0;i<list.size();i++){
				    	if(list.get(i).getName().equals(selectext2.getText())){
				    		model.addRow(new String[]{String.valueOf(list.get(i).getId()),list.get(i).getName(),String.valueOf(list.get(i).getPrice()),String.valueOf(list.get(i).getNum())});
				    	}
				    	
				    }
			}
				
			}
		});
		button_4.setBounds(197, 45, 93, 23);
		contentPane.add(button_4);
		
		JLabel Buylabel = new JLabel("购物车: 0 件商品");
		Buylabel.setBounds(10, 10, 124, 15);
		contentPane.add(Buylabel);
		
		JButton button_5 = new JButton("查看购物车");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				cf = new UserCartFrame();
				cf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				cf.setVisible(true);
			}
		});
		button_5.setBounds(116, 6, 110, 23);
		contentPane.add(button_5);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon(UserStore.class.getResource("/image/be6bda5a2c70d859a4483d559811ca38.jpg")));
		lblNewLabel.setBounds(0, 0, 716, 459);
		contentPane.add(lblNewLabel);
		
		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(true){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					File f=new File("D:\\Car\\");
					int n=f.list().length;
					Buylabel.setText("购物车: "+n+" 件商品");
				}
			}
			
		}).start();
		
	}
}
